<script setup>
import { ref, onMounted } from 'vue'
import CrawlerControl from '../components/CrawlerControl.vue'
import { useCrawlerStore } from '../stores/crawler'

const crawlerStore = useCrawlerStore()
const logs = ref([])

// 模拟日志数据
const mockLogs = [
  { time: '2023-07-01 10:00:00', level: 'info', message: '爬虫任务启动' },
  { time: '2023-07-01 10:00:05', level: 'info', message: '开始爬取图书列表页' },
  { time: '2023-07-01 10:02:30', level: 'warning', message: '遇到验证码，尝试自动识别' },
  { time: '2023-07-01 10:02:35', level: 'info', message: '验证码识别成功，继续爬取' },
  { time: '2023-07-01 10:30:00', level: 'info', message: '已爬取1000条图书数据' },
  { time: '2023-07-01 11:00:00', level: 'error', message: '请求频率过高，IP暂时被封禁' },
  { time: '2023-07-01 11:30:00', level: 'info', message: '切换代理IP，继续爬取' },
  { time: '2023-07-01 12:00:00', level: 'info', message: '爬虫任务完成，共爬取5000条数据' }
]

onMounted(() => {
  crawlerStore.fetchStatus()
  logs.value = mockLogs
})
</script>

<template>
  <div class="monitor">
    <h2>系统监控与日志</h2>
    
    <el-card shadow="hover" class="control-card">
      <template #header>
        <div class="card-header">
          <span>爬虫控制</span>
        </div>
      </template>
      <CrawlerControl />
    </el-card>
    
    <el-card shadow="hover" class="log-card">
      <template #header>
        <div class="card-header">
          <span>运行日志</span>
          <div class="log-actions">
            <el-button size="small">导出日志</el-button>
            <el-button size="small" type="primary">清空日志</el-button>
          </div>
        </div>
      </template>
      
      <div class="log-list">
        <div v-for="(log, index) in logs" :key="index" class="log-item" :class="log.level">
          <div class="log-time">{{ log.time }}</div>
          <div class="log-level" :class="log.level">
            {{ log.level.toUpperCase() }}
          </div>
          <div class="log-message">{{ log.message }}</div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.monitor {
  padding: 20px;
}

.control-card {
  margin-bottom: 20px;
}

.log-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.log-actions {
  display: flex;
  gap: 10px;
}

.log-list {
  max-height: 500px;
  overflow-y: auto;
}

.log-item {
  display: grid;
  grid-template-columns: 180px 80px 1fr;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
  font-size: 14px;
}

.log-item:last-child {
  border-bottom: none;
}

.log-time {
  color: #666;
}

.log-level {
  font-weight: bold;
  text-align: center;
}

.log-level.info {
  color: #67C23A;
}

.log-level.warning {
  color: #E6A23C;
}

.log-level.error {
  color: #F56C6C;
}

.log-message {
  color: #333;
}
</style>